<?php
/**
 * This file is part of Hyperf Mall.
 *
 * @link     https://gitee.com/scwlkj/hypref_mall
 * contact  hyperf_mall@163.com
 */
namespace App\JsonRpc;

use App\Cache\TokenCache;
use App\Repository\Member\MemberRepository;
use Hyperf\RpcServer\Annotation\RpcService;
use App\JsonRpc\Interfaces\MemberServiceInterface;

/**
 * @RpcService(name="Member::MemberService", protocol="jsonrpc", server="jsonrpc")
 */
class MemberService extends BaseService implements MemberServiceInterface
{
    public function init()
    {
        $this->repository = MemberRepository::getInstance();
    }

    /**
     * Notes: 会员中心
     * Author: Bruce.z
     * DateTime: 2022/7/19 13:52
     * @param int $member_id
     * @return array
     */
    public function info(int $member_id):array
    {
        return $this->repository->info($member_id);
    }

    public function index(array $condition, array $field, int $page, int $size): array
    {
        if(isset($condition['username']) && !empty($condition['username'])){
            $condition[] = ['username', 'like', '%'.trim($condition['username']).'%'];
            unset($condition['username']);
        }
        return parent::index($condition, $field, $page, $size);
    }

    /**
     * Notes: pc端会员注册
     * Author: Bruce.z
     * DateTime: 2022/9/26 9:41
     * @param array $data
     * @return mixed
     */
    public function registerPc(array $data)
    {
        return $this->repository->registerPc($data);
    }

    /**
     * Notes:pc端会员登录
     * Author: Bruce.z
     * DateTime: 2022/9/26 9:41
     * @param string $user_name
     * @param string $password
     * @return mixed
     */
    public function login(string $user_name, string $password)
    {
        return $this->repository->login($user_name, $password);
    }

    /**
     * Notes: 删除登录token
     * Author: Bruce.z
     * DateTime: 2022/9/26 15:30
     * @param $token
     * @return int
     */
    public function logout($token): int
    {
        return TokenCache::getInstance(TokenCache::TYPE_PC)->remove($token);
    }

    /**
     * Notes: pc 端重置密码
     * Author: Bruce.z
     * DateTime: 2022/10/19 20:13
     * @param int $member_id
     * @param string $oldPassword
     * @param string $newPassword
     * @return bool
     */
    public function resetPassword(int $member_id,string $oldPassword, string $newPassword): bool
    {
        return $this->repository->resetPassword($member_id, $oldPassword, $newPassword);
    }
}
